package org.syg.generator.util;

import org.syg.generator.entity.FreemarkerModel;

/**
 * @author ps
 */
public class Generator {

    /**
     * 路径信息
     */
    public static final String ENTITY="entity";
    public static final String DAO="dao";
    public static final String DAO_IMPL="Mapper.xml";
    public static final String SERVICE="service";
    public static final String SERVICE_IMPL="serviceImpl";
    public static final String CONTROLLER="controller";
    public static final String baseDir;

    static {
        baseDir = System.getProperty("user.dir") + "/src/";
    }

    /**
     * ①创建实体类
     * @param model
     */
    public static void createEntity(FreemarkerModel model) throws Exception {
        String fileUrl= getGeneratorFileUrl(model.getPackageDir(),model.getClassName(), ENTITY);
        FreemarkerUtil.createFile(model, "po.ftl", fileUrl);

        fileUrl= getGeneratorFileUrl(model.getPackageDir(),model.getClassName(), DAO_IMPL);
        FreemarkerUtil.createFile(model, "mapper-xml.ftl", fileUrl);
    }

    //生成文件路径和名字
    public static String getGeneratorFileUrl(String packageUrl,String entityName, String type){
        if (type.equals("entity")) {
            return baseDir+packageToPath(packageUrl)+entityName+".java";
        }else if(type.equals("dao")) {
            return baseDir+packageToPath(packageUrl)+entityName+"Dao.java";
        }else if(type.equals("Mapper.xml")) {
            return baseDir+packageToPath(packageUrl)+entityName+"Mapper.xml";
        }else if(type.equals("service")) {
            return baseDir+packageToPath(packageUrl)+entityName+"Service.java";
        }else if(type.equals("serviceImpl")) {
            return baseDir+packageToPath(packageUrl)+entityName+"ServiceImpl.java";
        }else if(type.equals("controller")) {
            return baseDir+packageToPath(packageUrl)+entityName+"Controller.java";
        }
        return null;
    }

    public static String packageToPath(String packageDir) {
        return packageDir.replace(".", "/")+"/";
    }
}
